<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ include file="/wesite/view/common/base.jsp" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>扫码采购</title>
    <%@ include file="/wesite/view/common/common.jsp" %>
    <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
</head>
<body>
<div class="scan-results">
    <div class="scan-head">
        <a style="display: block;" href="javascript:callWeixinScanQRCode();" class="rich-scan">扫一扫</a>
        <p style="color: #AAA;">点击 “ 扫一扫 ” 扫描商品条码</p>
    </div>
    <div class="scan-shop-list">

        <!-- 没商品 -->
        <div style="display: none;" class="noDataInfo scan-con-null">抱歉，未找到匹配的商品。</div>

        <!-- 有商品 -->
        <p style="display: none;" class="hasDataInfo scan-list-title bg-fff">为您找到以下商品：</p>
        <ul style="display: none;" id="productInfoList" class="hasDataInfo">
        </ul>
    </div>

    <div class="scan-btn">
        <a class="go-index fl" href="${ctx}/we/index">返回首页</a>
        <a class="go-cart fr" href="${ctx}/we/weshopcart">前往购物车</a>
    </div>
</div>

<%@ include file="/wesite/view/common/popup-menu.jsp" %>

<script type="text/javascript" src="${webctx}/script/aui-toast.js"></script>

<script>

    var toast = new auiToast({});

    function showToastMsg(type, msg) {

        switch (type) {
            case "success":
                toast.success({
                    title: msg,
                    duration: 2000
                });
                break;
            case "fail":
                toast.fail({
                    title: msg,
                    duration: 2000
                });
                break;
            case "info":
                toast.custom({
                    title: msg,
                    html: '<i class="aui-iconfont aui-icon-info"></i>',
                    duration: 2000
                });
                break;
            default:
                break;
        }
    }

    function showToastLoading(msg) {
        toast.loading({
            title: msg
        });
    }

    function hideToastLoading() {
        toast.hide();
    }

    var weixinScanQRCodeIsReady = false;
    $(function () {
        $.getJSON(apiroot + '/we/features/signature?token=' + "${SIGNATURE_TOKEN}" + "&url=" + window.location, function (cfg) {
            if (cfg && cfg.result == 'yes' && cfg.jsapi) {
                wx.config({
                    debug: false,
                    appId: cfg.jsapi.appId,
                    timestamp: cfg.jsapi.timestamp,
                    nonceStr: cfg.jsapi.nonceStr,
                    signature: cfg.jsapi.signature,
                    jsApiList: ['scanQRCode']
                });
                wx.ready(function () {
                    weixinScanQRCodeIsReady = true;
                });
                wx.error(function () {
                    showToastMsg("fail", "微信配置异常");
                });
            }
        });
    });

    function callWeixinScanQRCode() {
        if (weixinScanQRCodeIsReady) {
            wx.scanQRCode({
                needResult: 1,
                scanType: ['barCode'],
                success: function (res) {

                    var scanStr = res.resultStr;
                    if(scanStr.indexOf(",")>=0){
                        scanStr = scanStr.substring(scanStr.indexOf(",")+1);
                    }

                    if (scanStr) {
                        searchByBarcode(scanStr);
                    } else {
                        showToastMsg("fail", "未扫描到结果");
                    }
                },
                fail: function (res) {
                    showToastMsg("fail", "扫码失败");
                },
                cancel: function (res) {
                    showToastMsg("info", "扫码取消");
                }
            });
        } else {
            showToastMsg("info", "微信扫码<br/>正在准备");
        }
    }

    function searchByBarcode(barcode) {
        showToastLoading("正在查找...");
        var hideToastLoadingTimeout = window.setTimeout(function () {
            hideToastLoading();
        }, 30000);
        $.getJSON(apiroot + '/we/product/searchBarcode', {
            "barcode": barcode
        }, function (data) {
            if (data && data.MsgCode && data.MsgCode > 0) {
                buildProductInfo(data.Data);
            }
            hideToastLoading();
            window.clearTimeout(hideToastLoadingTimeout);
        });
    }

    function buildProductInfo(datas) {

        $(".hasDataInfo").hide();
        $(".noDataInfo").hide();
        $("#productInfoList").empty();
        if (datas && datas.length > 0) {
            var h = "";
            for (var i = 0; i < datas.length; i++) {
                var data = datas[i];

                h += "<li>";
                h += "<a class='scan-shop-img' href='${ctx }/we/product/" + data.id + "'><img src='" + getRandomFileserver() + data.photo + "' onerror='this.src='" + ctx + "/website/images/noimg.jpg''></a>";
                h += "<div class='scan-shop-text fl'>";
                h += "<p class='text-name'>" + data.productname + "</p>";
                h += "<p class='text-company txt'>" + data.b_chanjia + "</p>";
                h += "<p class='text-st txt'>" + data.b_guige + "</p>";

                if (data.iscontrol > 0) {
                    h += "<p class='text-iscontrol'>不在本区域销售</p>";
                    h += "<a class='ico-shopping-cart' href='javascript:void(0);' ><img src='/wesite/images/ico-shopping-cart-no.png'></a>";
                } else {
                    h += "<p class='text-price'>";
                    if (!islogin) {
                        h += "登录后可见";
                    } else if (!isuserauth) {
                        h += "认证后可见";
                    } else if (islogin && isuserauth && data.price) {
                        h += "￥";
                        h += data.price;
                        if (data.promotion_flash_sale) {
                            h += "<em>特价</em>";
                        }
                        if (data.promotion_buy_send) {
                            h += "<em>买送</em>";
                        }
                        if (data.promotion_money_off) {
                            h += "<em>满减</em>";
                        }
                        if (data.promotion_purchase_gift) {
                            h += "<em>满赠</em>";
                        }
                    }
                    h += "</p>";
                    if (data.amount > 0) {
                        h += "<a class='ico-shopping-cart' href='javascript:void(0);' onclick='stopBubble(event," + data.id + ")'><img src='/wesite/images/ico-shopping-cart.png'></a>";
                    } else {
                        h += "<a class='ico-shopping-cart' href='javascript:void(0);' ><img src='/wesite/images/ico-shopping-cart-no.png'></a>";
                    }
                }
                h += "</div>";
                h += "<div class='cl'></div>";
                h += "</li>";
            }
            $("#productInfoList").append(h);
            $(".hasDataInfo").show();
        } else {
            $(".noDataInfo").show();
        }
    }

</script>

</body>
</html>
